﻿using System;
using System.Collections.Generic;
using System.Text;
using Daybreak.Core.Network.Udp;

namespace Daybreak.Core.Network
{
    class SystemUdpConnectPacket : NetPacket
    {
        public override void ServerAction(ref NetBase server)
        {
            UdpNetServer tmp = (UdpNetServer)server;

            if (this.ClientID == -1)
            {
                tmp.count_connection_id += 1;

                this.Data = new byte[tmp.count_connection_id.ToString().Length];
                this.Data = Encoding.ASCII.GetBytes(tmp.count_connection_id.ToString());

                tmp.ClientOpenConnection(this.RemoteEndPoint, tmp.count_connection_id);

                tmp.Send(this);
            }
        }

        public override void ClientAction(ref NetBase client)
        {
            UdpNetClient unc = (UdpNetClient)client;

            unc.id = Convert.ToInt32(Encoding.ASCII.GetString(this.Data));
            unc.connected = true;
        }
    }

    class SystemUdpClosePacket : NetPacket
    {
        public override void ServerAction(ref NetBase server)
        {
            UdpNetServer tmp = (UdpNetServer)server;
            tmp.Send(this);
            tmp.ClientCloseConnection(this.ClientID);
        }

        public override void ClientAction(ref NetBase client)
        {
            UdpNetClient unc = (UdpNetClient)client;

            unc.connected = false;
        }
    }


    class SystemGetConfigFilePacket : NetPacket
    {
        public override void ServerAction(ref NetBase server)
        {
            this.Data = Encoding.ASCII.GetBytes(((UdpNetServer)server).Config);
            server.Send(this);
        }

        public override void ClientAction(ref NetBase client)
        {
            client.LoadPacketsFromConfig(Encoding.ASCII.GetString(this.Data));
            client.configLoaded = true;
        }
    }

    class SystemPing : NetPacket
    {

        public override void ServerAction(ref NetBase server)
        {
            server.Send(this);
        }

        public override void ClientAction(ref NetBase client)
        {
            double pingMillsec = double.Parse(Encoding.ASCII.GetString(this.Data));
            client.ping = client.TotalMilliseconds - pingMillsec;
        }
    }

    class SystemNewUserOnline : NetPacket
    {
        public override void ServerAction(ref NetBase server)
        {
        }

        public override void ClientAction(ref NetBase client)
        {
        }
    }
}
